/*
 * FaceCat图形通讯框架
 * 著作权编号:2015SR229355+2020SR0266727
 * 上海卷卷猫信息技术有限公司
 */

#include "../../stdafx.h"
#include "../../include/scroll/FCVScrollBar.h"

namespace FaceCat{
	FCVScrollBar::FCVScrollBar(){
	}

	FCVScrollBar::~FCVScrollBar(){
		FCButton *backButton = getBackButton();
		if(backButton){
			backButton->removeEvent((FCTouchEventCallBack*)this, L"ontouchdown");
			backButton->removeEvent((FCTouchEventCallBack*)this, L"ontouchup");
		}
	}

	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

	void FCVScrollBar::callTouchEvent(String eventName, Object sender, FCTouchInfo touchInfo, Object invoke){
		FCScrollBar::callTouchEvent(eventName, sender, touchInfo, invoke);
		if (sender == getBackButton()) {
			if (eventName == L"ontouchdown") {
				onBackButtonTouchDown(touchInfo);
			} else if (eventName == L"ontouchup") {
				onBackButtonTouchUp(touchInfo);
			}
		}
	}

	String FCVScrollBar::getViewType(){
		return L"VScrollBar";
	}

	void FCVScrollBar::onBackButtonTouchDown(FCTouchInfo touchInfo){
		FCButton *scrollButton = getScrollButton();
		FCPoint mp = touchInfo.m_firstPoint;
		if (mp.y < scrollButton->getTop()){
			pageReduce();
			setIsReducing(true);
		}
		else if (mp.y > scrollButton->getBottom()){
			pageAdd();
			setIsAdding(true);
		}
	}

	void FCVScrollBar::onBackButtonTouchUp(FCTouchInfo touchInfo){
		 setIsAdding(false);
         setIsReducing(false);
	}

	void FCVScrollBar::onDragScroll(){
		bool floatRight = false;
		FCButton *backButton = getBackButton();
		FCButton *scrollButton = getScrollButton();
		int backButtonHeight = backButton->getHeight();
		int contentSize = getContentSize();
		if(scrollButton->getBottom() > backButtonHeight){
			floatRight = true;
		}
		FCScrollBar::onDragScroll();
		if(floatRight){
			setPos(contentSize);
		}
		else{
			setPos((int)((Long)contentSize * (Long)scrollButton->getTop() / backButtonHeight));
		}
		onScrolled();
	}

	void FCVScrollBar::onLoad(){
		bool isAdd = false;
		FCButton *backButton = getBackButton();
		if (backButton){
			isAdd = true;
		}
		FCScrollBar::onLoad();
		if (!isAdd){
			backButton = getBackButton();
			backButton->addEvent((FCTouchEventCallBack*)this, L"ontouchdown", this);
			backButton->addEvent((FCTouchEventCallBack*)this, L"ontouchup", this);
		}
	}

	void FCVScrollBar::update(){
		if(m_native && !m_native->m_lockUpdate){
			FCButton *addButton = getAddButton();
			FCButton *backButton = getBackButton();
			FCButton *reduceButton = getReduceButton();
			FCButton *scrollButton = getScrollButton();
			int width = getWidth(), height = getHeight();
			int contentSize = getContentSize();
			if (contentSize > 0 && addButton && backButton && reduceButton && scrollButton){
				int pos = getPos();
				int pageSize = getPageSize();
				if (pos > contentSize - pageSize) {
					pos = contentSize - pageSize;
					m_pos = pos;
				}
				if (pos < 0) {
					pos = 0;
					m_pos = pos;
				}
				int abHeight = addButton->isVisible() ? addButton->getHeight() : 0;
				FCSize aSize = {width, abHeight};
				addButton->m_size = aSize;
				FCPoint aPoint = {0, height - abHeight};
				addButton->m_location = aPoint;
				int rbHeight = reduceButton->isVisible() ? reduceButton->getHeight() : 0;
				FCSize sSize = {width, rbHeight};
				reduceButton->m_size = sSize;
				FCPoint sPoint = {0, 0};
				reduceButton->m_location = sPoint;
				int backHeight = height - abHeight - rbHeight;
				FCSize bSize = {width, backHeight};
				backButton->m_size = bSize;
				FCPoint bPoint = {0, rbHeight};
				backButton->m_location = bPoint;
				int scrollHeight = backHeight * pageSize / contentSize;
				int scrollPos = (int)((Long)backHeight * (Long)pos / contentSize);
				if(scrollHeight < 10){
					scrollHeight = 10;
					if (scrollPos + scrollHeight > backHeight){
						scrollPos = backHeight - scrollHeight;
					}
				}
				FCSize scSize = {width, scrollHeight};
				scrollButton->m_size = scSize;
				FCPoint scPoint = {0, scrollPos};
				scrollButton->m_location = scPoint;
			}
		}
	}
}